<template>
  <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="roleDialog" @close="clonse">
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确认</el-button>
        <el-button size="small" @click="clonse">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addRolesInfo, byIdRelosInfo, updateRoleInfo } from '@/api/roles'
export default {
  props: {
    roleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        description: ''
      },
      curId: '',
      //   规则
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取指定角色信息
    async getByIdInfo(id) {
      this.curId = id
      const res = await byIdRelosInfo(id)
      this.formData = res.data
    },
    // 编辑/添加
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await updateRoleInfo(this.formData)
      } else {
        await addRolesInfo(this.formData)
      }
      this.$emit('updateInfo')
      this.$message({
        message: this.formData.id ? '更改成功' : '添加成功',
        type: 'success'
      })
      this.clonse()
    },
    // 关闭弹窗
    clonse() {
      this.$emit('update:roleDialog', false)
      this.formData = {
        title: '',
        description: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
